from django.db.models import Sum,Max,Min,Avg
from django.shortcuts import render

# Create your views here.
from grade.models import Grade, Student


def page_grade(request):
    '''学生的成绩统计'''
    # 练习1：求某个学生期末成绩的总和
    grade_list = Grade.objects.filter(student_name='张三')
    total = grade_list.aggregate(Sum('score'))
    # 练习2：求某一科目成绩的最高分/最低分
    grade_sub = Grade.objects.filter(subject_name='语文')
    max = grade_sub.aggregate(Max('score'))
    min = grade_sub.aggregate(Min('score'))
    # 练习3：求某一科目成绩的平均分
    grade_avg = Grade.objects.filter(subject_name='数学')
    avg = grade_avg.aggregate(Avg('score'))
    # 练习4：求每个学生期末成绩的总和
    stu_list = Student.objects.annotate(total=Sum('stu_grade__score'))
    return render(request, 'page_grade.html',{
        'total': total,
        'max': max,
        'min': min,
        'avg': avg,
        'stu_list': stu_list

    })